<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\CustHub\Amqp\Producers\RobotMsgProducers;
use App\CustHub\Mapper\ChContactMapper;
use Hyperf\Amqp\Producer;
use Hyperf\Di\Annotation\Inject;
use Mine\Abstracts\AbstractService;
use function Hyperf\Support\call;
use function Yansongda\Supports\data_get;

class RobotMessageService extends AbstractService
{

    #[Inject]
    protected ChContactMapper $contactMapper;

    public function sendMsg(array $data,$userId,$serverId,$robotKey)
    {

        $producer = container()->get(Producer::class);

        $multiMessageInterval = 0;

        foreach ($data as $item){
            $content = $item['content'];
            if(isset($item['limit'])){
                $limit = $item['limit'];
                $limitUnit = data_get($limit,'unit',1);
                $limitValue = data_get($limit,'value',10);
                $intervalTime = $this->getSeconds($limitUnit,$limitValue);
                $multiMessageInterval += $intervalTime;
            }
            $rst = $producer->produce(new RobotMsgProducers([
                'msgType' => $item['msg_type'],
                'content' => $content,
                'userId' => $userId,
                'serverId' => $serverId,
                'robotKey' => $robotKey
            ],$multiMessageInterval, 'high'));
        }

    }


    private function getSeconds($unit,$value)
    {
        switch ($unit){
            case 2:
                //分钟
                return $value * 60;
            case 3:
                //小时
                return $value * 60 * 60;
            default:
                //秒或者其他单位
                return $value;
        }
    }

    private function sendTextMsg($content,$robotUserIds){
    }
}